package tools  
{
	import flashx.textLayout.formats.Float;
	
	import org.flixel.FlxEmitter;
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxObject;
	import org.flixel.FlxParticle;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;
	import org.flixel.plugin.photonstorm.*;
	import org.flixel.plugin.photonstorm.BaseTypes.Bullet;
	import org.flixel.plugin.photonstorm.FX.BlurFX;
	
	public class Fx extends FlxGroup
	{
		public var explosions:FlxGroup;
		
		public function Fx()  { 
			super()
			if (FlxG.getPlugin(FlxSpecialFX) == null)
			{
				FlxG.addPlugin(new FlxSpecialFX);
			}
			
			explosions = new FlxGroup(40);
		}
		
		public function impact(bullet:Bullet, target:FlxObject):void {
			particles(bullet);
			bullet.kill();
		}
		/*
		public function explodeBlock(ax:int, ay:int):void {
			var emitter:FlxEmitter = explosions.recycle() as FlxEmitter;
			if(emitter) {
				emitter.x = ax;
				emitter.y = ay;
				emitter.start(true);
				add(emitter);
				emitter.start();
			} 
		}*/
		
		
		/** Build an Emmiter and then creates particles submited to the current gravity.
		 * 
		 * @param	target			where the particles comes from
		 * @param	color			the hexadecimal color code of each paticles
		 * @param	number	... hum... should I define that ?
		 * @param	size	the size of the particles in pixel
		 * */
		public function particles(target:FlxObject, color:uint = 0xFF777777, amount:uint = 10, size:Number = 3):void {	
			var emitter:FlxEmitter = new FlxEmitter(target.getMidpoint().x, target.getMidpoint().y, amount);
			emitter.gravity = Registry.gameInfos.world.gravity * .5	 ;
			emitter.lifespan = 1;
			
			for(var i:int = 0; i < amount ; i++) {
				var particle:FlxParticle = new FlxParticle();
                particle.makeGraphic(size, size, color);
				emitter.add(particle);
			}
			 
			add(emitter);
			emitter.start();
		}
		
		
		/** will shake the screen, with somhow like explosions ! 
		 * */
		public function shake(intensity:Number=0.01, duration:Number=0.2):void{
			FlxG.shake(intensity,duration);
		}

		
		/** this functions does not work yet 
		 * 
		 * It should apply a blur motion to the given FlxSprite... but it doesn't
		 * 
		 * */
		public function addBlur(target:FlxSprite):FlxSprite{
			
			var blur:BlurFX = FlxSpecialFX.blur();
			blur.start();
			//var blurEffect:FlxSprite;
			//blurEffect =
				
 			//blur.addSprite(target);
			//blurEffect.x = target.x;
			//blurEffect.y = target.y;
			//Registry.dynamicPool.add(blurEffect);
			
			return blur.create(target.width+50, target.height+50, 5,5, 6);
		}
		public function removeBlur():void{
			FlxSpecialFX.clear();
		}
		
		/** the target will be pushed away
		 * 
		 * @param	target 	the target wich recieve damages
		 * @sourceX	the origin of the damage (target will be pushed to the opposite direction
		 * 
		 * @return nothing
		 * 
		 * */
		public function kickHisAss(target:FlxObject, sourceX:Number = 0):void {
			var distance:Number;
			
			if (sourceX != 0) distance = (sourceX > target.x) ? -300 : 300;
			else  distance = (target.isTouching(FlxObject.RIGHT)) ? -300 : 300;
			
			target.velocity.x = distance;
			target.velocity.y = -300;
			
			return;
		}
		
	
	}

}